package com.onenine.app.activity;

import android.Manifest;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.cjt2325.cameralibrary.JCameraView;
import com.cjt2325.cameralibrary.listener.ClickListener;
import com.cjt2325.cameralibrary.listener.JCameraListener;
import com.cjt2325.cameralibrary.util.FileUtil;
import com.onenine.app.R;
import com.onenine.app.event.VideoFinishEvent;
import com.onenine.base.base.BaseActivity;
import com.onenine.base.utils.AppConfig;
import com.onenine.base.utils.StatusBarUtils;
import com.onenine.base.utils.Toasty;
import com.onenine.common.util.Logger;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import pub.devrel.easypermissions.AppSettingsDialog;
import pub.devrel.easypermissions.EasyPermissions;

/**
 * 视频录制
 */
public class VideoRecorderActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks, ClickListener, JCameraListener {

    private final String[] VIDEO_PERMISSIONS = {Manifest.permission.CAMERA, Manifest.permission.RECORD_AUDIO
            , Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE};
    private JCameraView jCameraView;
    private boolean granted = false;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        StatusBarUtils.setTransparent(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void setStatusBarStyle() {
    }

    @Override
    protected void initViews() {
        super.initViews();
        jCameraView = findViewById(R.id.jcameraview);
        //设置视频保存路径
        jCameraView.setTip(getString(R.string.video_recorder_long_press));
        jCameraView.setFeatures(JCameraView.BUTTON_STATE_ONLY_RECORDER);
        jCameraView.setLeftButtonVisible(false);
        jCameraView.setSaveVideoPath(AppConfig.VIDEO_CAMERA_DIR);
        jCameraView.setMaxDuration(AppConfig.VIDEO_MAX_DURATION);
    }

    @Override
    protected void initData() {
        super.initData();
        if (!EasyPermissions.hasPermissions(this, VIDEO_PERMISSIONS)) {
            granted = false;
            EasyPermissions.requestPermissions(this
                    , getString(R.string.permission_record_hint), 189, VIDEO_PERMISSIONS);
        } else {
            granted = true;
        }
    }

    @Override
    protected void initListeners() {
        super.initListeners();
        //JCameraView监听
        jCameraView.setQuitClickListener(this);
        jCameraView.setJCameraLisenter(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (granted) {
            jCameraView.onResume();
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        jCameraView.onPause();
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_video_recorder;
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        if (requestCode == 189 && perms.size() == VIDEO_PERMISSIONS.length) {
            granted = true;
            jCameraView.resumePermissionGranted();
        }
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        granted = false;
        if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {
            new AppSettingsDialog.Builder(this).setTitle(getString(R.string.permission_apply))
                    .setRationale(getString(R.string.permission_refuse_always_hint)).build().show();
        } else if (requestCode == 189) {
            Toasty.showNormal(R.string.permission_camera_hint);
            finish();
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, R.anim.slide_out_bottom);
    }

    @Override
    public void onClick() {
        finish();
    }

    @Override
    public void captureSuccess(Bitmap bitmap) {

    }

    @Override
    public void recordSuccess(String url, Bitmap firstFrame) {
        Logger.e("视频录制路径：" + url);
        String picPath = FileUtil.saveBitmap(AppConfig.VIDEO_CAMERA_DIR, firstFrame);
        EventBus.getDefault().post(new VideoFinishEvent(url, picPath));
        finish();
    }

}
